/*

    Copyright 2011 Cuong Le <metacuong@gmail.com>
  
    This package is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This package is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this package; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

*/

#include "droprestapi.h"
#include "json.h"

#include <QDebug>

DropRestAPI::DropRestAPI()
{
    oauth = new OAuth();
}

DropRestAPI::~DropRestAPI(){
    delete oauth;
}

QNetworkRequest DropRestAPI::request_token() {
    QUrl url(REQUEST_TOKEN_URL);
    QNetworkRequest rt;

    rt.setUrl(url);

    oauth->sign("GET", &rt);

    return rt;
}

QNetworkRequest DropRestAPI::request_access_token() {
    QUrl url(REQUEST_ACCESS_TOKEN);
    QNetworkRequest rt;

    rt.setUrl(url);

    oauth->sign("POST", &rt);

    return rt;
}

QNetworkRequest DropRestAPI::user_authentication(QString email, QString password){
    QUrl url = QString(TRUSTED_ACCESS_TOKEN_URL);
    QNetworkRequest rt;

    url.addQueryItem("email",email);
    url.addEncodedQueryItem("password",password.toUtf8().toPercentEncoding());
    url.addQueryItem("oauth_consumer_key", oauth->m_consumer_key);

    rt.setUrl(url);

    return rt;

}

QNetworkRequest DropRestAPI::root_dir(const QString &folder_name){
    QUrl url(QString("%1%2").arg(FILES_URL).arg(folder_name));
    QNetworkRequest rt;

    rt.setUrl(url);

    oauth->sign("GET", &rt);

    return rt;
}

void DropRestAPI::oauth_request_token_reply_process(QNetworkReply *networkreply){
    QList<QByteArray> oauth_content = networkreply->readAll().split('&');

    oauth->m_secret = oauth_content.at(0).split('=').at(1);
    oauth->m_token = oauth_content.at(1).split('=').at(1);
}

void DropRestAPI::oauth_user_authentication_reply_process(QNetworkReply *networkreply){
    QVariantMap jsonmap= Json::parse(networkreply->readAll()).toMap();

    oauth->m_token = QString("%1").arg(jsonmap["token"].toString()).toAscii();
    oauth->m_secret = QString("%1").arg(jsonmap["secret"].toString()).toAscii();
}

QNetworkRequest DropRestAPI::file_transfer(QString filename, QString dropbox_folder, QString boundaryStr){
   QUrl url;
   QNetworkRequest rt;

   url.setUrl(QString("%1%2").arg(FILES_TRANSFER_URL).arg(dropbox_folder));
   url.addQueryItem("file", filename);

   rt.setUrl(url);
   rt.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundaryStr);

   oauth->sign("POST", &rt);

   return rt;
}

QNetworkRequest DropRestAPI::file_transfer_download(QString dropbox_filepath){
    QUrl url = QString("%1%2").arg(FILES_TRANSFER_URL).arg(dropbox_filepath);
    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::__delete(QString dropbox_filepath){
    QUrl url = QString("%1").arg(FILE_DELETE_URL);
    url.addQueryItem("root","dropbox");
    url.addQueryItem("path",dropbox_filepath);
    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::__create(QString dropbox_filepath){
    QUrl url = QString("%1").arg(CREATE_FOLDER_URL);
    url.addQueryItem("root","dropbox");
    url.addQueryItem("path",dropbox_filepath);
    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::__move(QString path_source, QString path_destination){
    QUrl url = QString("%1").arg(FILE_MOVE_URL);
    url.addQueryItem("root", "dropbox");
    url.addQueryItem("from_path", path_source);
    url.addQueryItem("to_path", path_destination);

    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::__copy(QString path_source, QString path_destination){
    QUrl url = QString("%1").arg(FILE_COPY_URL);
    url.addQueryItem("root", "dropbox");
    url.addQueryItem("from_path", path_source);
    url.addQueryItem("to_path", path_destination);

    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::__shares(QString dropbox_filepath){
    QUrl url = QString("%1").arg(SHARES_URL);
    url.addQueryItem("root","dropbox");
    url.addQueryItem("path",dropbox_filepath);
    QNetworkRequest rt(url);
    oauth->sign("POST", &rt);
    return rt;
}

QNetworkRequest DropRestAPI::accountinfo() {
    QUrl url = QString("%1").arg(ACCOUNT_INFO_URL);
    QNetworkRequest rt(url);
    oauth->sign("GET", &rt);
    return rt;
}
